// miniprogram/pages/personPage/issue/issue.js
Page({

    /**
     * 页面的初始数据
     */
    data: {
        list: [],
        checkList: [],
        del: false,
        delList: []

    },
    onChange(event) {
        this.setData({
            checkList: event.detail
        });
    },
    all() {
        if (this.data.checkList.length == this.data.list.length) {
            this.setData({
                checkList: []
            })
        } else {
            let temporary = []
            this.data.list.forEach(element => {
                temporary.push(element._id)
            });
            this.setData({
                checkList: temporary
            })
        }

    },
    delItem() {
        let that = this
        if (this.data.del == true && this.data.checkList.length == 0) {
            this.setData({
                del: false
            })
        } else if (this.data.del == false) {
            this.setData({
                del: true
            })
        } else if (this.data.del == true && this.data.checkList.length != 0) {
            wx.showModal({
                title: '提示',
                content: '确定下架',
                success(res) {
                    if (res.confirm) {
                        wx.showLoading({
                            mask: true,
                            title: '删除中',
                        })
                        let dataList = []
                        that.data.checkList.forEach(element => {
                            for (const key in that.data.list) {
                                if (element == that.data.list[key]._id) {
                                    dataList.push({
                                        _id: element,
                                        type: that.data.list[key].types
                                    })
                                }
                            }
                        });

                        wx.cloud.callFunction({
                            name: 'issue',
                            data: {
                                list: dataList,
                            }
                        }).then((data) => {
                            if (data.result == "ok") {
                                that.setData({
                                    checkList: []
                                })
                                that.getList()
                            } else {//删除失败
                                wx.showToast({
                                    title: '错误',
                                    icon: 'none',
                                    image: '/pages/images/icon/error.png',
                                    duration: 2000
                                })
                            }
                        })
                    }
                }
            })
        }

    },
    gotoDetails(e) {
        // console.log(e.currentTarget.dataset);
        if (e.currentTarget.dataset.item.title) {
            wx.navigateTo({
                url: '/pages/homePage/usedGoods/details/details',
                success: function (res) {
                    // 通过eventChannel向被打开页面传送数据
                    res.eventChannel.emit('to_details', {
                        mes: e.currentTarget.dataset.item,
                        status: "2"
                    })
                }
            })
        } else if (e.currentTarget.dataset.item.type) {
            wx.navigateTo({
                url: '/pages/homePage/lostGoods/lostGoods_details/lostGoods_details',
                success: function (res) {
                    // 通过eventChannel向被打开页面传送数据
                    res.eventChannel.emit('to_details', {
                        mes: e.currentTarget.dataset.item,
                        status: "2"
                    })
                }
            })
        } else {
            wx.navigateTo({
                url: '/pages/homePage/money/moneydetail/moneydetail',
                success: function (res) {
                    // 通过eventChannel向被打开页面传送数据
                    res.eventChannel.emit('to_details', {
                        mes: e.currentTarget.dataset.item,
                        status: "2"
                    })
                }
            })
        }

    },
    getList() {
        wx.cloud.callFunction({
            name: 'issue',
        }).then(data => {
            let list = []

            for (const key in data.result) {
                const element = data.result[key];
                for (const item in element.list) {
                    element.list[item].types = element.types
                    if (element.list[item].time) {
                        element.list[item].time = element.list[item].time.slice(0, 10)
                    }
                    list.push(element.list[item])
                }
            }
            this.setData({
                list: list.reverse(),
            })
            wx.hideLoading()
        })

    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {

    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {
        wx.showLoading({
            mask: true,
            title: '加载中',
        })
        this.getList()
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})